home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c
- Subject: Re: Interesting declaration???
- Date: 24 Jan 1996 14:56:54 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan24095654@g7240065.bridge.bst.bls.com>
- References: <4e3on1$iir@masala.cc.uh.edu>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: sukku@menudo.uh.edu's message of 23 Jan 1996 22:50:09 GMT
-
- In article <4e3on1$iir@masala.cc.uh.edu> sukku@menudo.uh.edu (sukumar) writes:
-
- I was going through some of the header files of Motif and found an
- : interesting declaration like this:
-
- : typedef enum{
- : NONE,
- : ONE,
- : TWO} TabelType;
- ^^
- I'll presume this was a typo.
-
- : #define TableType unsigned char
-
-
- : I haven't seen this kind of declartion before and I tried it after seeing this
- : and it worked.!! I was expecting the compiler to throw a flag. Why would
- : someone do this??
-
- : My best guess is that they are making sure the TableType gets only one
- : byte instead of four (On Unix).
-
- : Can someone throw some light on this??
-
- There is no reason for this not to compile:
-
- typedef enum { NONE, ONE, TWO } TableType;
-
- #define TableType unsigned char
-
- TableType flag = ONE;
-
- After the preprocessor stage looks like.
-
- typedef enum { NONE, ONE, TWO } TableType;
-
- unsigned char flag = ONE;
-
- Enums are compatible with an integer type (char/short/int/long), the choice
- of the type is implementation defined.
- When the compiler sees the flag initialisation it performs a conversion
- (if necessary) from the enum to unsigned char.
- The standard states that in this instance NONE=0, ONE=1 and TWO=2
- so there is no problem with this.
-
- There may be several reasons for doing this:
-
- Size maybe important, char types take one byte enums can take more.
- If this is being used in network communication (like most of X) then
- the size of the enum on one machine maybe different on another machine,
- it allows one to rationalise about the its size which can't be
- done with enums.
- The enumeration are bit flags and require or'ing together (probably not
- in this case) '|' is not defined on enums and must be converted to
- an integer type.
- And more...
-
- The reason for doing it the way it was done allows users to still,
- declare things using TableType with out having to be care of what
- is actually going on.
-
- Regards
-
- -A.
- --
- | A.Champion |
-